package com.yhn.registration.controller;

import com.hospital.common.base.Result;
import com.hospital.common.dto.registration.CreateRegistrationDto;
import com.hospital.common.pojo.TRegistration;
import com.yhn.registration.service.RegistrationService;
import com.yhn.registration.vo.RegistrationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 挂号预约控制器
 *
 * @author yhn
 * @version 1.0
 */
@RestController
@RequestMapping("/api/registration")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    /**
     * 创建挂号预约
     */
    @PostMapping("/create")
    public Result<TRegistration> createRegistration(@RequestBody CreateRegistrationDto dto) {
         return registrationService.createRegistration(dto);
    }

    /**
     * 根据患者ID查询挂号记录
     */
    @GetMapping("/listByPatient")
    public Result<java.util.List<TRegistration>> listByPatient(@RequestParam("patientId") Long patientId) {
        return registrationService.listByPatientId(patientId);
    }

    /**
     * 医生统计当天预约人数
     */
    @GetMapping("/doctor/dailyAppointments")
    public Result<Integer> getDoctorDailyAppointments() {
        return registrationService.getDoctorDailyAppointments();
    }

    /**
     * 医生统计自己进行中与已完成的挂号数量
     */
    @GetMapping("/doctor/activeCount")
    public Result<Integer> countDoctorActiveRegistrations() {
        return registrationService.countDoctorActiveRegistrations();
    }

    /**
     * 取消当前挂号（调用支付取消，回滚号源，状态改为2）
     */
    @GetMapping("/cancelAndRestore")
    public Result<String> cancelAndRestore(@RequestParam("patientId") Long patientId,
                                           @RequestParam("registrationId") Long registrationId) {
        return registrationService.cancelAndRestore(patientId, registrationId);
    }

    /**
     * 医生查看各状态数量统计
     */
    @GetMapping("/doctor/stateCount")
    public Result<java.util.Map<String, Integer>> countDoctorRegistrationsByState() {
        return registrationService.countDoctorRegistrationsByState();
    }

    /**
     * 患者查看各状态数量统计（全部时间）
     */
    @GetMapping("/patient/stateCount")
    public Result<java.util.Map<String, Integer>> countPatientRegistrationsByState() {
        return registrationService.countPatientRegistrationsByState();
    }

    /**
     * 根据医生id查询预约患者信息
     */
    @GetMapping("/doctor/query")
    public Result<List<RegistrationVo>> queryByDoctorId() {
        return Result.success(registrationService.queryByDoctorId());
    }

    /**
     * 分页查询患者预约记录
     * @param state 预约状态（可选，0=待支付，1=已支付，2=已取消，3=未就诊）
     * @param pageNum 页码（从1开始）
     * @param pageSize 每页大小（默认10，最大100）
     * @return 分页结果
     */
    @GetMapping("/patient/listByPage")
    public Result<com.hospital.common.base.PageResult<TRegistration>> listPatientRegistrationsByPage(
            @RequestParam(value = "state", required = false) Integer state,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return registrationService.listPatientRegistrationsByPage(state, pageNum, pageSize);
    }

    /**
     * 后台管理 - 分页查询所有预约记录（支持多条件查询）
     * @param patientName 患者姓名（可选）
     * @param doctorName 医生姓名（可选）
     * @param department 科室（可选）
     * @param state 预约状态（可选，0=待支付，1=已支付，2=已取消，3=未就诊）
     * @param startTime 预约开始时间（可选）
     * @param endTime 预约结束时间（可选）
     * @param pageNum 页码（从1开始）
     * @param pageSize 每页大小（默认10，最大100）
     * @return 分页结果
     */
    @GetMapping("/admin/listByPage")
    public Result<com.hospital.common.base.PageResult<TRegistration>> listAllRegistrationsByPage(
            @RequestParam(value = "patientName", required = false) String patientName,
            @RequestParam(value = "doctorName", required = false) String doctorName,
            @RequestParam(value = "department", required = false) String department,
            @RequestParam(value = "state", required = false) Integer state,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return registrationService.listAllRegistrationsByPage(patientName, doctorName, department, state, startTime, endTime, pageNum, pageSize);
    }

    /**
     * 后台管理 - 获取预约统计信息
     * @return 统计结果
     */
    @GetMapping("/admin/stats")
    public Result<java.util.Map<String, Object>> getRegistrationStats() {
        return registrationService.getRegistrationStats();
    }

    /**
     * 后台管理 - 根据ID查看预约详情
     * @param id 预约记录ID
     * @return 预约详情
     */
    @GetMapping("/admin/detail/{id}")
    public Result<TRegistration> getRegistrationDetail(@PathVariable("id") Long id) {
        return registrationService.getRegistrationDetail(id);
    }

    /**
     * 医生端：确定就诊（state=1 完成）
     */
    @GetMapping("/doctor/confirmVisited")
    public Result<String> confirmVisited(@RequestParam("registrationId") Long registrationId) {
        return registrationService.confirmVisited(registrationId);
    }

    /**
     * 医生端：未就诊（state=3 未就诊）
     */
    @GetMapping("/doctor/markNotVisited")
    public Result<String> markNotVisited(@RequestParam("registrationId") Long registrationId) {
        return registrationService.markNotVisited(registrationId);
    }

    /**
     * 统计当日预约总数
     */
    @GetMapping("/admin/today/count")
    public Result<Integer> countTodayAppointments() {
        return registrationService.countTodayAppointments();
    }

    /**
     * 医生按条件查询患者，支持分页，按预约时间降序排列
     */
    @GetMapping("/doctor/searchPatients")
    public Result<com.hospital.common.base.PageResult<RegistrationVo>> doctorSearchPatients(
            @RequestParam(value = "patientName", required = false) String patientName,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return registrationService.doctorSearchPatients(patientName, startTime, endTime, pageNum, pageSize);
    }

    /**
     * 医生查询今日所有患者（按预约时间升序排列），支持分页
     */
    /**
     * 医生查询今日所有患者（按预约时间升序排列）
     */
    @GetMapping("/doctor/today/list")
    public Result<java.util.List<RegistrationVo>> doctorListTodayPatients() {
        return registrationService.doctorListTodayPatients();
    }

}

